import * as Cesium from 'cesium';
import './cesium-tilesetEffect'
import "cesium/Build/Cesium/Widgets/widgets.css";
import { point } from './BeiJingMap'

let viewer

const MapCesiumConfig = {
    accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIwMTg2MjU5YS1kMmYxLTRhYjItYmYwYi0xN2NkZjM1ZmM4MzAiLCJpZCI6ODU5NzMsImlhdCI6MTY0NzQxNzY2MX0.co6YpzRKMkURwYYTlU0jaq6D-aPHUpIlQOh2fIdqT18',
    init(container = 'container') {
        window.CESIUM_BASE_URL = '/';

        Cesium.Ion.defaultAccessToken = MapCesiumConfig.accessToken

        viewer = new Cesium.Viewer(container, {
            baseLayerPicker: false, // 隐藏右上角影像拾取
            animation: false, // 隐藏左下角的动画仪表盘
            fullscreenButton: false, // 隐藏全屏按钮
            homeButton: false, // 隐藏home按钮
            geocoder: false, // 隐藏右上角搜索
            sceneModePicker: false, // 隐藏2d3d切换按钮
            timeline: false, // 隐藏时间轴
            navigationHelpButton: false, // 隐藏导航帮助按钮
            terrainProvider: Cesium.createWorldTerrain()
        });

        // viewer.scene.globe.show = false;
        viewer.imageryLayers.get(0).show = false;//不显示底图
        viewer.scene.globe.baseColor = Cesium.Color.WHITE;//设置地球颜色

        viewer._cesiumWidget._creditContainer.style.display= 'none'

        // 添加3D建筑
        viewer.scene.primitives.add(Cesium.createOsmBuildings())
        // 2.飞行到北京，并添加坐标点
        this.cesiumFlyTo(117.3, 39.9, 500000, this.addPoint)
        // 3.添加地图影像
        this.addImageryLayers()
        // 5.添加遮罩层
        this.addMask()

        // 加载3dtileset
        // let tilesets = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
        //     url: 'https://lab.earthsdk.com/model/702aa950d03c11e99f7ddd77cbe22fea/tileset.json' //切片url
        // }))
        // tilesets.readyPromise.then(function (tilesetObj) {
        //     tilesetObj.style = new Cesium.Cesium3DTileStyle({
        //         color: {
        //             conditions: [
        //                 ["true", "color('cyan')"]
        //             ]
        //         }
        //     });
        //     viewer.flyTo(tilesetObj)
        // })
    },
    addImageryLayers () {
        // 加载天地图影像
        // const imgMap = new Cesium.UrlTemplateImageryProvider({
        //     url: 'http://t{s}.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=0673de02879f1a6755a804089b971d32',
        //     subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
        //     tilingScheme: new Cesium.WebMercatorTilingScheme(),
        //     maximumLevel: 18
        // })
        //天地图矢量
        const imgMap = new Cesium.WebMapTileServiceImageryProvider({
            url:'http://{s}.tianditu.gov.cn/vec_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=vec&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=0673de02879f1a6755a804089b971d32',
            layer:'vec',
            style:'default',
            format:'tiles',
            tileMatrixSetID:'w',
            subdomains:['t0','t1','t2','t3','t4','t5','t6','t7'],
            credit:new Cesium.Credit('天地图矢量'),
            maximumLevel:18
        })
        viewer.imageryLayers.addImageryProvider(imgMap)
    },
    cesiumFlyTo (x, y, height, complete) {
        // 飞行到哪个位置
        viewer.camera.flyTo({
            destination: Cesium.Cartesian3.fromDegrees(x, y, height),
            duration: 8, // 多少秒完成跳转
            complete: complete // 完成后的执行函数
        })
    },
    addPoint () {
        // 添加坐标点
        viewer.entities.add({
            position: Cesium.Cartesian3.fromDegrees(116.20, 39.56),
            point: {
                pixelSize: 30,
                color: Cesium.Color.RED
            }
        })
        // 添加文字
        viewer.entities.add({
            position: Cesium.Cartesian3.fromDegrees(116.328327, 39.945162),
            label: {
                text: '北京',
                font: '24px Helvetica',
                fillColor: Cesium.Color.SKYBLUE,
                outlineColor: Cesium.Color.BLACK,
                outlineWidth: 2,
                style: Cesium.LabelStyle.FILL_AND_OUTLINE
            }
        })
    },
    addMask () {
        // 添加遮罩层,遮罩层是按照顺序叠加起来的会被覆盖，如果透明则会变色
        const entities = viewer.entities
        let stripeMaterial = new Cesium.StripeMaterialProperty({
            evenColor: Cesium.Color.YELLOW.withAlpha(0.5),
            oddColor: Cesium.Color.YELLOW.withAlpha(0.5),
            repeat: 5.0
        })
        entities.add({
            polygon: {
                hierarchy: new Cesium.PolygonHierarchy(
                    Cesium.Cartesian3.fromDegreesArray(point.c100010)
                ),
                outline: true,
                outlineColor: Cesium.Color.WHITE,
                outlineWidth: 4,
                material: stripeMaterial
            }
        })
        stripeMaterial = new Cesium.StripeMaterialProperty({
            evenColor: Cesium.Color.BLUE.withAlpha(0.5),
            oddColor: Cesium.Color.BLUE.withAlpha(0.5),
            repeat: 5.0
        })
        entities.add({
            polygon: {
                hierarchy: new Cesium.PolygonHierarchy(
                    Cesium.Cartesian3.fromDegreesArray(point.c100000)
                ),
                outline: true,
                outlineColor: Cesium.Color.WHITE,
                outlineWidth: 4,
                material: stripeMaterial
            }
        })
    }
}

export default MapCesiumConfig

/**
 * 切换地图
 */
function changeBaseMap(type) {
    viewer.imageryLayers.removeAll();
    switch (type) {
        //天地图
        case "tdt":
            viewer.imageryLayers.addImageryProvider(
                new WebMapTileServiceImageryProvider({
                    url:
                        "https://t0.tianditu.com/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=0673de02879f1a6755a804089b971d32",
                    layer: "tdtBasicLayer",
                    style: "default",
                    format: "image/jpeg",
                    tileMatrixSetID: "GoogleMapsCompatible",
                    show: false,
                    maximumLevel: 16
                })
            );
            break;
        //天地图矢量
        case "tdtsl":
            viewer.imageryLayers.addImageryProvider(
                new WebMapTileServiceImageryProvider({
                    url:
                        "https://t0.tianditu.com/vec_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=vec&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=0673de02879f1a6755a804089b971d32",
                    layer: "tdtVecBasicLayer",
                    style: "default",
                    format: "image/jpeg",
                    tileMatrixSetID: "GoogleMapsCompatible",
                    show: false
                })
            );
            break;
        //谷歌影像
        case "gg":
            var url =
                "https://mt1.google.cn/vt/lyrs=s&hl=zh-CN&x={x}&y={y}&z={z}&s=Gali";
            viewer.imageryLayers.addImageryProvider(
                new UrlTemplateImageryProvider({ url: url })
            );
            break;
        case "arcgis":
            viewer.imageryLayers.addImageryProvider(
                new ArcGisMapServerImageryProvider({
                    url:
                        "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
                    enablePickFeatures: false
                })
            );
            break;
        //必应
        case "bing":
            viewer.imageryLayers.addImageryProvider(
                new BingMapsImageryProvider({
                    url: "https://dev.virtualearth.net",
                    key: "get-yours-at-https://www.bingmapsportal.com/",
                    mapStyle: BingMapsStyle.AERIAL
                })
            );
            break;
        case "dark":
            viewer.imageryLayers.addImageryProvider(
                new createTileMapServiceImageryProvider({
                    url: "https://cesiumjs.org/blackmarble",
                    credit: "Black Marble imagery courtesy NASA Earth Observatory",
                    flipXY: true // Only old gdal2tile.py generated tilesets need this flag.
                })
            );
            break;
    }

    //全球影像中文注记服务
    viewer.imageryLayers.addImageryProvider(
        new WebMapTileServiceImageryProvider({
            url:
                "https://t0.tianditu.com/cia_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=cia&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default.jpg&tk=93d1fdef41f93d2211deed6d22780c48",
            layer: "tdtAnnoLayer",
            style: "default",
            format: "image/jpeg",
            tileMatrixSetID: "GoogleMapsCompatible",
            show: false
        })
    );
    //全球矢量中文标注服务
    viewer.imageryLayers.addImageryProvider(
        new WebMapTileServiceImageryProvider({
            url:
                "https://t0.tianditu.com/cva_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=cva&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default.jpg&tk=93d1fdef41f93d2211deed6d22780c48",
            layer: "tdtAnnoLayer",
            style: "default",
            format: "image/jpeg",
            tileMatrixSetID: "GoogleMapsCompatible"
        })
    );
}